<template v-if="visible">
    <a-modal v-model:open="visible" :title="title" @ok="handleOk">
        <a-form
            ref="formRef"
            :model="formState"
            :rules="rules"
            :label-col="{ span: 6 }"
            autocomplete="off"
            v-if="visible"
        >
            <a-form-item label="县名字" name="countyName">
                <a-input v-model:value="formState.countyName" />
            </a-form-item>
            <a-form-item label="总销量" name="totalSales">
                <a-input v-model:value="formState.totalSales" addon-after="万元" />
            </a-form-item>
            <a-form-item label="县销量榜第一店" name="countySaleFirst">
                <a-input v-model:value="formState.countySaleFirst" />
            </a-form-item>
            <a-form-item label="榜首销量" name="firstSales">
                <a-input v-model:value="formState.firstSales" addon-after="万元" />
            </a-form-item>
            <a-form-item label="县网店量" name="OnlineStore">
                <a-input v-model:value="formState.OnlineStore" addon-after="个" />
            </a-form-item>
            <a-form-item label="县实体店量" name="offlineStore">
                <a-input v-model:value="formState.offlineStore" addon-after="个" />
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from 'vue'
import type { FormInstance } from 'ant-design-vue'
import type { Ref } from 'vue'
import Lodash from 'lodash'

import { toast } from '@/utils/common'
import { Edit } from '../../store'
import type { ItemResponse } from '../../type'

const defaultFormState = {
    id: '',
    countyName: '',
    totalSales: '',
    countySaleFirst: '',
    firstSales: '',
    OnlineStore: '',
    offlineStore: '',
    notes: ''
}

export default defineComponent({
    name: 'EditDialog',
    emits: ['refresh'],

    setup(props, { emit }) {
        const state = reactive({
            visible: false,
            title: '',
            formState: Lodash.cloneDeep(defaultFormState)
        })

        const constants = {
            rules: {} as any
        }
        for (let key in defaultFormState) {
            constants.rules[key] = [{ required: true, message: '请输入！' }]
        }
        
        const components = {
            formRef: ref<FormInstance>()
        } as {
            formRef: Ref<FormInstance>
        }
        const methods = {
            open(row?: ItemResponse) {
                state.title = row?.id ? '编辑' : '新增'
                console.log(row)

                if (row) {
                    state.formState = Lodash.cloneDeep(row)
                } else {
                    state.formState = Lodash.cloneDeep(defaultFormState)
                }
                state.visible = true
            },
            async handleOk() {
                const isOK = await components.formRef.value.validateFields()
                if (!isOK) return
                let params = Lodash.cloneDeep(state.formState)
                console.log(params)

                const res: any = await Edit(params)
                if (res.status === 201) {
                    toast('err', '', res.result.msg)
                    return
                }
                emit('refresh')
                state.visible = false
                toast()
            }
        }
        return {
            ...toRefs(state),
            ...methods,
            ...constants,
            ...components
        }
    }
})
</script>
